var express = require('express');
var router = express.Router();
var sqlUtils = require('../sql/sqlUtil');
let Result = require('../util/utils');
/* GET home page. */
router.get('/', function (req, res, next) {
  res.redirect("/login.html");
});
// 拦截修改密码请求
router.put('/', function (req, res) {
  var id = req.body.uid,
    old = req.body.old_password,
    newpassword = req.body.new_password,
    again = req.body.again_password;

  if (newpassword != again) {
    res.send(Result.setResult(200, 0));
  } else if (old === again) {
    res.send(Result.setResult(200, 1));
  } else {
    var sql = "select password from users where id=" + id;
    sqlUtils.sqlConnect(sql, function (err, data) {
      if (err) {
        console.log(err);
      } else {
        if (data[0].password == old) {
          sqlUtils.sqlConnect("update users set password='" + again + "' where id=" + id, function (err, data) {
            if (err) {
              console.log(err);
            } else {
              res.send(Result.setResult(200, "success"));
            }
          })
        } else {
          res.send(Result.setResult(200, 2));
        }
      }
    })
  }

})
// 拦截index请求
router.get('/index.html', function (req, res, next) {
  if (!req.session.isLogin) {
    res.redirect("login.html");
  } else {
    res.render('index', {
      username: req.session.username,
      uid: req.session.uid,
      name: req.session.name,
      gender: req.session.gender,
      mobile: req.session.mobile,
      role: req.session.role,
      schools:req.session.schools
    });
  }
})
// 拦截login.html
router.get('/login.html', function (req, res, next) {
  if (!req.session.isLogin) {
    res.render("login", {});
  } else {
    res.redirect('index.html');
  }
})
// 账号验证
// router.post('/checkUser',function(req,res,next){
//   var username=req.body.username;
//   var sql="select * from users where username=? and is_del=0";
//   sqlUtils.sqlConnect(sql,[username],function(err,data){
//     if(data.length > 0){
//       res.send(Result.setResult(200,1));
//     }else {
//       res.send(Result.setResult(200,0));
//     }
//   })
// })
// 登录
router.post('/login', function (req, res, next) {
  var username = req.body.username;
  var password = req.body.password;
  console.log(req.body);
  var sql = "select * from users where username=? and is_del=0";
  var sqlArr = [username];
  sqlUtils.sqlConnect(sql, sqlArr, function (err, data) {
    if (data.length > 0) {
      if (data[0].password == password) {
        req.session.isLogin = true;
        req.session.username = username;
        req.session.uid = data[0].id;
        req.session.name = data[0].name;
        req.session.gender = data[0].gender;
        req.session.mobile = data[0].mobile;
        req.session.role = data[0].role;
        req.session.schools = data[0].schools;
        res.send(Result.setResult(200, 1));
      } else {
        res.send(Result.setResult(200, 0));
      }
    } else {
      res.send(Result.setResult(200, 2));
    }
  })
})
//退出登录
router.get('/clear', function (req, res) {
  req.session.destroy(); //设置销毁session对象
  res.sendStatus(200)
})

module.exports = router;